package com.chookss.video

import android.content.Intent
import android.view.View
import com.chookss.R
import com.chookss.base.StaticClass
import com.chookss.tools.MyEvent
import com.chookss.tools.Utils
import com.chookss.video.entity.VideoEntity
import com.chookss.video.model.VideoSourceModel
import com.chookss.video.util.VideoUtils
import com.chookss.view.BottomListDialog
import com.johnrambo.ktea.ktExtends.ShareUtils
import com.johnrambo.ktea.ktExtends.observe
import errorToast
import kotlinx.android.synthetic.main.activity_base_video_play.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Company:
 * Project:
 * Author: liuhaoge
 * Date: 2021/1/20 16:17
 * Note:
 */
class VideoSetsPlayActivity : BaseVideoPlayActivity() {

    private val videoList = ArrayList<VideoSourceModel>()
    private lateinit var videoEntity: VideoEntity
    private var type: String = "1" //1可以直接到集合2不能直接跳转集合  3 自己的可以直接到集合 4学习任务过来直接播放集合
    private var isTaskComplete = false //播放时间可以达到完成任务了
    private var index:Int=0  //播放所在下标
    private var effTime: String = ""
    private var invalidTime: String = ""
    override fun initOnCreate(): Boolean {
        var tmp = intent?.getSerializableExtra("video")
        type = intent?.getStringExtra("type").toString()
        effTime = intent?.getStringExtra("effTime").toString()
        invalidTime = intent?.getStringExtra("invalidTime").toString()
        if (tmp == null) {
            return false
        }
        videoEntity = tmp!! as VideoEntity
        return true
    }

    override fun initVideoPlayView() {
        super.initVideoPlayView()
        tvTitle.visibility = View.GONE
        videoPlayView.videoAdapter.setOnUpdateHolder { holder, position, entity ->
            if (!Utils.isNull(entity.videoCollectionsCode)) {
                var num = 1
                if (!Utils.isNull(entity.videoSort)) {
                    num = entity.videoSort.toInt()
                }
                holder.tvSetsInfo.visibility = View.VISIBLE
                holder.tvSetsInfo.text = "合集·" + entity.videoCollectionsName
            }
            holder.tvMmarquee.visibility = View.GONE


        }
    }

    private fun findVideoIndex(video: VideoEntity): Int {
        for (i in 0 until videoList.size) {
            if (video.videoId == videoList[i].video.videoId) {
                index=i
                return i
            }
        }
        return 0
    }

    //    var dialog: BottomListDialog? =null
    private fun showSetsDialog(model: VideoEntity) {
        var position: Int =findVideoIndex(model)
        val listVideos: List<VideoEntity> = videoList.map { it.video }

        var dialog = BottomListDialog(
            this,
            model.videoCollectionsName + "·更新至第" + listVideos.get(listVideos.size - 1).videoSort + "集"
        )
        val adapter = VideoInSetsAdapter()
        adapter.datas = listVideos
        adapter.selectInt = position
        dialog?.setItems(adapter, position) { dialog, which ->
            dialog.dismiss()
            videoPlayView.videoListView.addPlayCount()
            videoPlayView!!.scrollToPosition(which)
        }
        dialog?.show()


    }

    private fun showSetsDialog2(model: VideoEntity) {
        val intent = Intent(
            this,
            VideoSetsPlayActivity::class.java
        )
        intent.putExtra("video", model)
        intent.putExtra("type", "1")
        intent.putExtra("watermark", "4")
        startActivity(intent)
    }

    override fun createOnVideoButtonClickListener(): OnVideoButtonClickListener {
        return object : OnVideoButtonClickListener(videoModel, this) {
            override fun onVideoClick(v: View, tvComment: View, position: Int, model: VideoEntity) {
                if (v.id == R.id.tvSetsInfo) {
                    if ("1".equals(type) || "3".equals(type) || "4".equals(type)) {
                        showSetsDialog(model)
                    } else {
                        showSetsDialog2(model)
                    }

                    return
                }
                super.onVideoClick(v, tvComment, position, model)
            }
        }
    }
    override fun loadPlayList(isLoadMoreData: Boolean) {

        if (videoList.isNotEmpty()) {
            handler.postDelayed({
                videoPlayView!!.completeLoadState(true)
            }, 1)
        } else {
            if("4".equals(type)){
                videoModel!!.getVideoByNewCollectionsCode(type,effTime,invalidTime, videoEntity.videoCollectionsCode)
                    .observe(this, {
                        if (it.error != null) {
                            if (videoPlayView != null) {
                                videoPlayView.loadFailure()
                            }
                            errorToast(it.error?.message ?: "其他错误")
                        } else {
                            videoList.clear()
                            if (it.data?.size ?: 0 > 0) {
                                videoList.addAll(it.data!!.map { VideoUtils.videoEntityToModel(it) })
                                if("4".equals(type)){
                                    for (i in 0 until videoList.size) {
                                        videoList.get(i).video.isTask="1"
                                    }
                                }

                            }
                            videoPlayView!!.refreshVideoList(
                                videoList,
                                findVideoIndex(videoEntity),
                                true
                            )

                        }
                    })
            }else{
                videoModel!!.getVideoByCollectionsCode(type, videoEntity.videoCollectionsCode,
                    ShareUtils().getString(mContext!!, StaticClass.EMPLOYEECODE, ""))
                    .observe(this, {
                        if (it.error != null) {
                            if (videoPlayView != null) {
                                videoPlayView.loadFailure()
                            }
                            errorToast(it.error?.message ?: "其他错误")
                        } else {
                            videoList.clear()
                            if (it.data?.size ?: 0 > 0) {
                                videoList.addAll(it.data!!.map { VideoUtils.videoEntityToModel(it) })
                            }
                            videoPlayView!!.refreshVideoList(
                                videoList,
                                findVideoIndex(videoEntity),
                                true
                            )

                        }
                    })
            }

        }
    }

    override fun initData() {
        ivLeft.setOnClickListener {
            videoPlayView.videoListView.addPlayCount()
            finish()
        }
    }

    override fun onBackPressed() {
        videoPlayView.videoListView.addPlayCount()
        super.onBackPressed()
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    override fun onMoonEvent(messageEvent: MyEvent) {
        //事件订阅者处理事件
        if (messageEvent.hashMap != null) {
            val map: Map<String, String?> = messageEvent.hashMap
        } else {
            val msg = messageEvent.message
            if (msg?.equals("DeleteVideo")!!) {
                finish()
            }
        }
    }
}